<template>
  <bsCard v-loading="loading" class="separate-wrapper" :width="getBsLRWPercent(745)" :height="`${cardHeight}px`">
    <div class="bs-card-title">注册地与经营地分离</div>
    <div class="separate-content">
      <div class="separate-l">
        <div class="total">
          {{ num }}
          <span>家</span>
        </div>
        <div v-show="tbRate">同比 {{ tbRate }}</div>
        <div v-show="hbRate">环比 {{ hbRate }}</div>
      </div>
      <div class="separate-m">
        <div class="location">崂山</div>
        <div class="arrowbox">
          <div>{{ outTotal }} 家</div>
          <div class="arrow"></div>
          <div>迁出</div>
        </div>
      </div>
      <ul class="separate-r">
        <li v-for="(per,index) of listOut" :key="index">
          <div>{{ per.toArea }}</div>
          <div>{{ per.entNum }}</div>
        </li>
      </ul>
    </div>
    <app-echarts height="120px" :options="echartsOption" />
  </bsCard>
</template>

<script>
import bsCard from './bsCard.vue'
import appEcharts from './appEcharts.vue'
import {
  getSeparationOfRegisteredPlaceAndBusinessPlace,
  getTypesOfEnterpriseMigration
} from '@/api/bigscreen'
export default {
  name: 'separate',
  components: {
    bsCard,
    appEcharts
  },
  inject: ['getBsLRWPercent'],
  data() {
    return {
      cardHeight: 320,
      loading: true,
      //
      num: '',
      tbRate: '',
      hbRate: '',
      listOut: [],
      //
      echartsOption: {
        // title: {
        //   text: '企业迁移类型分布',
        //   textStyle: {
        //     color: '#fff'
        //   }
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: []
        },
        grid: {
          top: '10px',
          left: '0',
          right: '0',
          bottom: '0',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: [],
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            nameTextStyle: {
              color: '#fff'
            },
            axisLabel: {
              rotate: 10
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            nameTextStyle: {
              color: '#fff'
            },
            splitLine: {
              show: false
            }
          }
        ],
        series: [
          {
            name: '迁入',
            type: 'bar',
            barGap: 0,
            emphasis: {
              focus: 'series'
            },
            itemStyle: {
              color: '#67BCBD'
            },
            barWidth: 20,
            data: []
          },
          {
            name: '迁出',
            type: 'bar',
            barGap: 0,
            emphasis: {
              focus: 'series'
            },
            itemStyle: {
              color: '#B36574'
            },
            barWidth: 20,
            data: []
          }
        ]
      }
    }
  },
  computed: {
    outTotal() {
      return this.listOut.reduce((total, item) => {
        return (total += +item.entNum)
      }, 0)
    }
  },
  async created() {
    this.loading = true
    await this.fetchSeparatData()
    await this.fetchTypesData()
    this.loading = false
  },
  methods: {
    async fetchSeparatData() {
      try {
        const res = await getSeparationOfRegisteredPlaceAndBusinessPlace()
        if (res.resultCode === 200) {
          //
          const data = res.resultData
          this.num = data.count[0] ? data.count[0].num : 0
          this.tbRate = data.count[0].tbRate
          this.hbRate = data.count[0].hbRate
          this.listOut = data.listOut.sort((a,b)=>{
            return +b.entNum - +a.entNum
          })
        }
      } catch (error) {
        console.log(error)
      }
    },
    async fetchTypesData() {
      try {
        const res = await getTypesOfEnterpriseMigration()
        if (res.resultCode === 200) {
          //
          this.renderType(res.resultData || [])
        }
      } catch (error) {
        console.log(error)
      }
    },
    renderType(data) {
      const typeArr = []
      const qrData = []
      const qcData = []
      for (let perdata of data) {
        if (perdata.hyType === '其它行业') continue
        typeArr.push(perdata.hyType)
        qrData.push(perdata.entNumQr)
        qcData.push(perdata.entNumQc)
      }

      // xAxis
      this.echartsOption.xAxis[0].data = typeArr
      // series
      this.echartsOption.series[0].data = qrData
      this.echartsOption.series[1].data = qcData
    }
  }
}
</script>

<style lang='scss'>
@import '../styles/_variables.scss';
.separate-wrapper {
  .separate-content {
    height: 150px;
    display: flex;
    justify-content: space-between;
    font-size: 18px;
  }

  .separate-l {
    display: flex;
    flex: 1;
    flex-flow: column;
    justify-content: center;
    text-align: center;
    .total {
      color: $FontBlue;
      font-size: 35px;

      span {
        font-size: 18px;
      }
    }
  }

  .separate-m {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .location {
      font-size: 30px;
      font-weight: 800;
      color: $FontBlue;
      line-height: 80px;
      padding-left: 40px;
      background-image: url('../assets/location.png');
      background-size: 30px auto;
      background-repeat: no-repeat;
      background-position: 5px center;
    }

    .arrowbox {
      flex: 1;
      display: flex;
      flex-flow: column;
      justify-content: center;
      align-items: center;
    }

    .arrow {
      position: relative;
      margin: 10px 0;
      height: 15px;
      background-image: url('../assets/arrow.png');
      background-repeat: no-repeat;
      background-size: 100% auto;
      background-position: center center;
      width: calc(100% - 20px);
    }
  }

  .separate-r {
    flex: 0 0 auto;
    border-top: 1px solid $FontBlue;
    height: 100%;
    overflow-y: scroll;
    li {
      display: flex;
      border-bottom: 1px solid $FontBlue;
      white-space: nowrap;

      div {
        flex: 1;
        padding: 2px 20px 2px 30px;
        text-align: center;
      }

      div:last-child {
        border-left: 1px dashed $FontBlue;
        padding-right: 30px;
        padding-left: 20px;
      }
    }
  }
}
</style>

